package com.pentajock.docdb.gui;

import com.pentajock.docdb.Main;
import com.pentajock.docdb.types.User;
import com.pentajock.docdb.database.DBController;
import com.pentajock.docdb.utils.Utils;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableRenderer extends DefaultTableCellRenderer {
	private String departmentName = null;
	
	public TableRenderer() {
	}
	
	public TableRenderer(String userName) {
		departmentName = DBController.getUserDepartment(userName);
	}
	
	public TableRenderer(User u) {
		departmentName = u.getDepartmentName();
	}
	
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
	    Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
	    try {
		    Utils.rendererHelper(this, value, departmentName, isSelected, table);
	    }
	    catch (Exception e) {
		    Main.printStackTrace(e, "Error adding " + value.toString() + " to table");
	    }
	    return c;
	}

	public void setUserName(String userName) {
		departmentName = DBController.getUserDepartment(userName);
	}
}
